package com.bool.example.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * Swagger2配置
 * @author bool 
 * @date 2020-02-29 11:22
 */
@Configuration
@EnableSwagger2
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {

    private static final String BASE_PACKAGE = "com.bool.example";
    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket open() {
        return new Docket(DocumentationType.SWAGGER_2)
                //注册整体api信息
                .globalOperationParameters(headers())
                .apiInfo(apiInfo())
                .groupName("default")
                //swagger功能是否启用
                .enable(enableSwagger)
                .select()
                //指定扫描的包
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.ant("/api/v1/**"))
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("项目接口")
                .description("这是项目接口的描述名称")
                .contact(new Contact("bool", "", ""))
                .version("1.0.0")
                .build();
    }

    /**
     * 头部token标识
     * @return
     */
    private List<Parameter> headers(){

        ParameterBuilder token = new ParameterBuilder();
        token.name("token")
                .description("用户登录会话标识，用户相关的操作必须传递")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        ParameterBuilder platform = new ParameterBuilder();
        platform.name("platform")
                .description("APP终端,取值为：ios和android")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        ParameterBuilder version = new ParameterBuilder();
        version.name("version")
                .description("APP版本号,初版默认为：v1.0.0")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        List<Parameter> params = Lists.newArrayList();
        params.add(token.build());
        params.add(platform.build());
        params.add(version.build());

        return params;
    }

}